/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mooman219.gmail.server.gui;

import java.awt.event.KeyEvent;
import java.util.ArrayList;

import com.mooman219.gmail.server.manager.network.ServerPacketManager;
import com.mooman219.gmail.shared.packet.Packet1Chat;

/**
 *
 * @author Mooman
 */
public class ServerInterface extends javax.swing.JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    /**
     * Creates new form ServerInterface
     */
    public ServerInterface() {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ServerInterface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ServerInterface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ServerInterface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ServerInterface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        initComponents();
    }

    public void addLine(String line){
        mainText.append("\n" + line);
        mainText.setCaretPosition(mainText.getText().length());
    }

    public void updateUserList(ArrayList<String> names){
        userList.setText("");
        for(String name : names) {
            userList.append(name + "\n");
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        inputField = new javax.swing.JTextField();
        splitPane = new javax.swing.JSplitPane();
        userListScrollPane = new javax.swing.JScrollPane();
        userList = new javax.swing.JTextArea();
        mainTextScrollPane = new javax.swing.JScrollPane();
        mainText = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        inputField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                inputFieldKeyPressed(evt);
            }
        });

        splitPane.setDividerLocation(this.getWidth() - 160);
        splitPane.setDividerSize(2);
        splitPane.setResizeWeight(1.0);
        splitPane.setContinuousLayout(true);

        userListScrollPane.setMinimumSize(new java.awt.Dimension(1, 1));
        userListScrollPane.setPreferredSize(new java.awt.Dimension(180, 100));

        userList.setColumns(13);
        userList.setEditable(false);
        userList.setRows(5);
        userList.setDragEnabled(true);
        userList.setPreferredSize(new java.awt.Dimension(130, 100));
        userListScrollPane.setViewportView(userList);

        splitPane.setRightComponent(userListScrollPane);

        mainTextScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        mainTextScrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        mainTextScrollPane.setAutoscrolls(true);
        mainTextScrollPane.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        mainTextScrollPane.setMinimumSize(new java.awt.Dimension(10, 10));

        mainText.setColumns(20);
        mainText.setEditable(false);
        mainText.setLineWrap(true);
        mainText.setRows(5);
        mainText.setWrapStyleWord(true);
        mainText.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        mainText.setDragEnabled(true);
        mainTextScrollPane.setViewportView(mainText);

        splitPane.setLeftComponent(mainTextScrollPane);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(splitPane, javax.swing.GroupLayout.DEFAULT_SIZE, 674, Short.MAX_VALUE)
                                .addComponent(inputField))
                                .addContainerGap())
                );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(splitPane, javax.swing.GroupLayout.DEFAULT_SIZE, 438, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(inputField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void inputFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_inputFieldKeyPressed
        if(inputField.getText().length() == 0) {
            return;
        }
        if(evt.getKeyCode() == KeyEvent.VK_ENTER){
            String message = inputField.getText();
            inputField.setText("");
            addLine("Server: " + message);
            ServerPacketManager.sendGlobalPacket(new Packet1Chat("Server: " + message));
        }
    }//GEN-LAST:event_inputFieldKeyPressed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField inputField;
    private javax.swing.JTextArea mainText;
    private javax.swing.JScrollPane mainTextScrollPane;
    private javax.swing.JSplitPane splitPane;
    private javax.swing.JTextArea userList;
    private javax.swing.JScrollPane userListScrollPane;
    // End of variables declaration//GEN-END:variables
}
